//
//  QJServiceHelper.m
//  QJWork
//
//  Created by 高中雷 on 15/5/30.
//  Copyright (c) 2015年 高中雷. All rights reserved.
//

#import "QJServiceHelper.h"
@implementation QJServiceHelper


+(QJServiceHelper*)serviceHelper{
    static QJServiceHelper * service = NULL;
    
    if (!service) {
        service = [[[self class] alloc] init];
        
    }
    return service;
}


-(NSDictionary *)getConfigWithName:(NSString *)strName configType:(NSString *)fileType
{
    return [self dictFromConfigName:strName configType:fileType];
}

#pragma mark - 获取host方法
-(NSString *)getLocalHostNameWithClassName:(NSString *)className{
//   NSDictionary *hostDic =  [self getConfigWithName:@"setting" configType:@"plist"];
//    NSString *host = [[hostDic objectForKey:className] objectForKey:@"host"];
    return APPIP;
    
}


#pragma mark - 私有方法 专门负责根据名字读取plist或者json
-(NSDictionary *)dictFromConfigName:(NSString *)configName configType:(NSString *)fileType
{
    NSDictionary *pDict = NULL;
    NSString *path;
    
    // m_dictConfigCache缓存字典
    if (!self.m_dictConfigCache) {
        self.m_dictConfigCache = [NSMutableDictionary dictionary];
    }
    
    pDict = [self.m_dictConfigCache objectForKey:configName];
    
    if (pDict) {
        return pDict;
    }
    
    NSFileManager *fmgr = [NSFileManager defaultManager];
    path = [[NSBundle mainBundle] pathForResource:configName ofType:fileType];
    if (!(path && [fmgr fileExistsAtPath:path])) return nil;
    
    if ([fileType isEqualToString:@"plist"]) {
        pDict = [[NSDictionary alloc] initWithContentsOfFile:path];
        
    } else if ([fileType isEqualToString:@"json"]) {
        NSData *jsonData = [NSData dataWithContentsOfFile:path];
        pDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    }
    
    if (pDict) {
        [self.m_dictConfigCache setObject:pDict forKey:configName];
    }
    
    return pDict;
}


+(NSDictionary*)errorCode:(int)nCode withString:(NSString*)strError withData:(NSString*)strData withHttpCode:(int)nHttpCode {
    if (strData)
        return @{ @"errorCode": [NSNumber numberWithInt:nCode], @"errorString" : strError, @"errorData" : strData, @"httpCode" : [NSNumber numberWithInt:nHttpCode] };
    return @{ @"errorCode": [NSNumber numberWithInt:nCode], @"errorString" : strError, @"httpCode" : [NSNumber numberWithInt:nHttpCode] };
}


@end
